package ZDloader;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;

import javax.swing.JOptionPane;

// A first attempt to run robocopy (arguments are passed static)
// Tested and working. Copies D:/test folder and contents to D:/test2

public class Robocopy {
	public static long overAllSize;
	public static long doneBytes;
		//dns: Arguments for Robocopy class
	  public static void main(String source, String dest) throws Exception {
		//dns: set the progress of the bar to 1
	    int progress = 1;
	    Gui.jProgressBar1.setValue(progress);
		//dns: Set the source folder to get the overall size
	    File Filesource = new File(source);
	    //dns: get the overall size
  	  	retrieveTotalBytes(Filesource);
	    //dns: show the overall size of the files that are about to be copied
  	  	JOptionPane.showMessageDialog(null,"Overall size " + overAllSize);

  	  	
		//dns: run robocopy proccess
		ProcessBuilder pb = new ProcessBuilder("robocopy.exe", source, dest , "-S");
		pb.redirectErrorStream(true);
		Process proc = pb.start();

		InputStream is = proc.getInputStream();
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(isr);

		String line;
		int exit = -1;
		
		while ((line = br.readLine()) != null) {
		    // Outputs robocopy execution
		    System.out.println(line);
		   
		    try {
		        exit = proc.exitValue();
		        if (exit != 0){
		        	
		        	 //dns: Percentage Done
		        	File FileDest = new File(dest);
			    	retrieveDoneBytes(FileDest);
			    	File FileSource = new File(source);
			    	retrieveTotalBytes(FileSource);
			    	//String LeftBytes = overAllSize - doneBytes;
			    	byte[] OverSize = longToBytes(overAllSize);
			    	byte[] DoneSize = longToBytes(doneBytes);
			    	float PercentDone = (float)((doneBytes*100)/overAllSize);
			    	int PercDone = Math.round(PercentDone);
			    	Gui.jProgressBar1.setValue(PercDone);
		        }
		        if (exit == 0)  {
		            // Process finished
		        	System.out.println("Finished");
		        	
		        	
		        }
		        
		        
		    } catch (IllegalThreadStateException t) {
		        // The process has not yet finished. 
		        // Should we stop it?
		    	
	            
		    }
		}
		
		
		//Gui.jProgressBar1.setValue(100);
			//There is no need for absolute path, windows 7 will have this in system env
			//there for, we only call robocopy
	    
	    //dns: debugg message showing the dirs copied
	   
	    
  	  	
	  }
	  private static void retrieveTotalBytes(File sourceFile)
      {
		  File[] files = sourceFile.listFiles();
          for(File file : files)
          {
              if(file.isDirectory()) retrieveTotalBytes(file);
              else overAllSize += file.length();
          }
      }
	  private static void retrieveDoneBytes(File sourceFile)
      {
		  File[] files = sourceFile.listFiles();
          for(File file : files)
          {
              if(file.isDirectory()) retrieveTotalBytes(file);
              else doneBytes = file.length();
          }
      }
	  public static int compareByteArrays(byte[] overAllSize, byte[] doneBytes) {
		  int n = Math.min(overAllSize.length, doneBytes.length), nLarge = Math.max(overAllSize.length, doneBytes.length);
		  int unequalCount = nLarge - n;
		  for (int i=0; i<n; i++) 
		    if (overAllSize[i] != doneBytes[i]) unequalCount++;
		  return (int) (unequalCount * 100.0 / nLarge);
		}
	  
	  public static byte[] longToBytes(long x) {
		    ByteBuffer buffer = ByteBuffer.allocate(Long.SIZE);
		    buffer.putLong(x);
		    return buffer.array();
		}

}

